﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;

using Microsoft.SharePoint;
using Microsoft.SharePoint.Utilities;
using Microsoft.SharePoint.WebControls;

using Kiiro.Common.Entities;
using Kiiro.Common.Utility;

namespace Kiiro.SiteDefinition.ControlTemplates
{
    public partial class Pager : System.Web.UI.UserControl
    {
        #region Pager Control Public Events
        
        public event EventHandler NextClick;
        public event EventHandler PrevClick;
        public event EventHandler EntitiesPerPageChange;

        #endregion

        #region Pager Control Properties

        public string SortExpression
        {
            get { return ViewState["SortExpression"] == null ? "Title" : ViewState["SortExpression"].ToString(); }
            set { ViewState["SortExpression"] = value; }
        }

        public SortDirection SortDirection
        {
            get { return ViewState["SortDirection"] == null ? SortDirection.Ascending : (SortDirection)ViewState["SortDirection"]; }
            set { ViewState["SortDirection"] = value; }
        }

        public int MinRange
        {
            get { return ViewState["MinRange"] == null ? 0 : (int)ViewState["MinRange"]; }
            set { ViewState["MinRange"] = value; }
        }

        public int MaxRange
        {
            get { return ViewState["MaxRange"] == null ? 0 : (int)ViewState["MaxRange"]; }
            set { ViewState["MaxRange"] = value; }
        }
        
        public int PageCounter
        {
            get { return ViewState["PageCounter"] == null ? 1 : (int)ViewState["PageCounter"]; }
            set { ViewState["PageCounter"] = value; }
        }

        public int TotalNumberOfEntities
        {
            get { return ViewState["TotalNumberOfEntities"] == null ? 0 : (int)ViewState["TotalNumberOfEntities"]; }
            set { ViewState["TotalNumberOfEntities"] = value; }
        }

        private string entityDisplayed = string.Empty;
        public string EntityDisplayed
        {
            get { return entityDisplayed; }
            set { entityDisplayed = value; }
        }
        
        private int pagingIndex = default(int);
        public int PagingIndex
        {
            get { return pagingIndex; }
            set { pagingIndex = value; }
        }

        private int displayedCount = default(int);
        public int DisplayedCount
        {
            get { return displayedCount; }
            set { displayedCount = value; }
        }  
   
        private int displayRange = default(int);
        public int DisplayRange
        {
            get { return displayRange; }
            set { displayRange = value; }
        }

        private int currentPage = default(int);
        public int CurrentPage
        {
            get { return currentPage; }
            set { currentPage = value; }
        }
        
        private int lastPage = default(int);
        public int LastPage
        {
            get { return lastPage; }
            set { lastPage = value; }
        }

        private bool isSortClick = false;
        public bool IsSortClick
        {
            get { return isSortClick; }
            set { isSortClick = value; }
        }

        private bool isFilter = false;
        public bool IsFilter
        {
            get { return isFilter; }
            set { isFilter = value; }
        }
        
        #endregion

        #region Pager Control Methods
        
        internal void PopulatePager(int totalCount, string entityType)
        {
            entityDisplayed = entityType;
            TotalNumberOfEntities = totalCount;

            PageCounter = GetPageNumber();
            displayRange = SetDisplayRange(totalCount);
            if (!IsSortClick)
            {
                int oldMaxRange = MaxRange;
                MaxRange = displayRange * PageCounter;
                MaxRange = MaxRange > TotalNumberOfEntities ? TotalNumberOfEntities : MaxRange;
                if ((MaxRange - oldMaxRange) > 0)
                    MinRange = MaxRange - (MaxRange - oldMaxRange) + 1;
                else
                    MinRange = MaxRange - displayRange + 1;
                MinRange = MinRange > 0 ? MinRange : 1;
                MinRange = MinRange > MaxRange ? 1 : MinRange;
            }

            pagingIndex = MinRange - 1 ;

            if (!IsSortClick)
            {
                displayedCount = MaxRange - pagingIndex;
            }

            if (IsFilter)
            {
                pagingIndex = 0;
                MaxRange = displayRange;
                MinRange = 1;
                DisplayedCount = MaxRange;
            }

            currentPage = (int)Math.Ceiling(((double)pagingIndex + 1) / displayRange);
            lastPage = (int)Math.Ceiling((double)TotalNumberOfEntities / displayRange);

            if (PageCounter <= 1)
                lnkPrevious.Visible = false;
            else
                lnkPrevious.Visible = true;

            if (MaxRange >= TotalNumberOfEntities)
                lnkNext.Visible = false;
            else
                lnkNext.Visible = true;

            if (ddlEntitiesPerPage.SelectedItem.Text == "ALL" || TotalNumberOfEntities < int.Parse(ddlEntitiesPerPage.SelectedItem.Text))
            {
                pagerPerPage.Text = "ITEMS PER PAGE ";
                pagerDisplaying.Text = "1 of 1 ";
                lnkPrevious.Visible = false;
                lnkNext.Visible = false;
            }
            else
            {
                pagerPerPage.Text = "ITEMS PER PAGE ";
                pagerDisplaying.Text = CurrentPage + " of " + LastPage + " ";
            }
        }

        protected void lnkPrevious_Click(object sender, EventArgs e)
        {
            if (PageCounter > 1)
            {
                PageCounter--;            
            }
            PrevClick(sender, e);
        }

        protected void lnkNext_Click(object sender, EventArgs e)
        {
            if (MaxRange < TotalNumberOfEntities)
            {
                PageCounter++;
            }
            NextClick(sender,e);
        }

        protected void ddlEntitiesPerPage_SelectedIndexChanged(object sender, EventArgs e)
        {
            MinRange = 1;
            if (ddlEntitiesPerPage.SelectedItem.Text != "ALL")
                MaxRange = int.Parse(ddlEntitiesPerPage.SelectedItem.Text);
            else
                MaxRange = TotalNumberOfEntities;
            PageCounter = 1;
            EntitiesPerPageChange(sender, e);
        }

        internal int SetDisplayRange(int count)
        {
            int displayRange = default(int);
            if (ddlEntitiesPerPage.SelectedItem.Text != "ALL")
            {
                if (count < int.Parse(ddlEntitiesPerPage.SelectedItem.Text))
                {
                    displayRange = count;
                }
                else
                {
                    displayRange = int.Parse(ddlEntitiesPerPage.SelectedItem.Text);
                }
            }
            else
            {
                displayRange = count;
            }
            return displayRange;
        }

        internal int GetPageNumber()
        {
            return PageCounter;
        }

        #endregion                
    }
}

/*************************************************************************
 * 
 * $Log: /Internal Projects/Kiiro/Kiiro.SiteDefinition/12/TEMPLATE/CONTROLTEMPLATES/Kiiro/Pager.ascx.cs $
 * 
 * 15    1/06/10 5:35p Camerons
 * 
 * 14    12/07/09 3:16p Stefanf
 * 
 * 13    12/02/09 12:29p Stefanf
 * 
 * 12    30/09/09 9:37a Shafia
 * 
 * 11    25/09/09 10:51a Shafia
 * 
 * 10    25/09/09 10:44a Shafia
 * Pager now displays number of pages rather than number of items
 * 
 * 9     24/09/09 2:41p Shafia
 * 
 * 8     22/09/09 10:09a Shafia
 * 
 * 7     21/09/09 3:36p Shafia
 * 
 * 6     16/09/09 10:50a Shafia
 * 
 * 5     16/09/09 10:20a Shafia
 * 
 * 4     17/08/09 11:07a Camerons
 * Added check-in comments section
 * 
 ************************************************************************/